<template>
    <view class="">
        <gb-choujiang :nImg='nImg' :dataId='dataId' :AwardList="list" :sjNum="sjNum" @updateMoney="updateMoney"></gb-choujiang>
		<u-popup border-radius='30' v-model="prizeShow"  mode="center" width='600' height="500">
			<view class="prizeTittle">
				恭喜你，获得以下奖品
			</view>
			<view class="center" style="margin-top: 20rpx;">
				<image :src="prizeData.prizeImg" mode="widthFix" style="width: 100rpx;"></image>
				<!-- <img :src="prizeData.prizeImg" alt="" srcset="" > -->
				
			</view>
			<view class="cneter" style="text-align: center;font-size: 30rpx;">
				{{prizeData.prizeName}}
			</view>
			<view class="center">
				<u-button size="medium" shape="square" type="warning" @click="toOrder()">绑定地址</u-button>
			</view>
		</u-popup>	
    </view>
</template>

<script>
	import { jackpotDetail,lottery } from '@/tyapi/address.js'
    export default {
        data() {
            return {
				prizeShow:false,
				prizeData:{},
				show:false,
				dataId:'',
				jackpotData:{},
                // 背景图，可网络地址，可本地
                // nImg:'../../static/luck.png',
				nImg:'http://123.56.189.119:9000/manghe/%E5%A5%96%E6%B1%A0%E8%83%8C%E6%99%AF_20231107230808.png',
                // 中奖的索引，也就是AwardList数组的索引，sjNum不能等于4，4是立即抽奖
                sjNum: 0,
                // false前端，true后端
                isQd:true,
				list:[],
                // 可自己后端获取，也可以后端  percentage为概率 10代表10%   100代表100%   注意：必须要有一个概率为100%，
                AwardList: [{
                        image: '../../static/smdj.png',
                        name: '神秘大奖',
                        percentage:0,
                        index_num:0
                    },
                    {
                        image: '../../static/fhb.png',
                        name: '0.5个金币',
                        percentage:30,
                        index_num:1
                    },
                    {
                        image: '../../static/fhb.png',
                        name: '0.1个金币',
                        percentage:100,
                        index_num:2
                    },
                    {
                        image: '../../static/fhb.png',
                        name: '1.00个金币',
                        percentage:30,
                        index_num:3
                    },
                    {
                        image: '../../static/begin.png',
                        name: '1.00元/次',
                        percentage:-1, 
                        index_num:4
                    },
                    {
                        image: '../../static/fhb.png',
                        name: '0.50个金币',
                        percentage:20,
                        index_num:5
                    },
                    {
                        image: '../../static/fhb.png',
                        name: '1.00个金币',
                        percentage:10,
                        index_num:6
                    },
                    {
                        image: '../../static/fhb.png',
                        name: '5.00个金币',
                        percentage:0,
                        index_num:7
                    },
                    {
                        image: '../../static/dehb.png',
                        name: '大额红包',
                        percentage:0,
                        index_num:8
                    }
                ]
            }
        },
       onLoad(option) { //option为object类型，会序列化上个页面传递的参数
       		console.log(option.id); //打印出上个页面传递的参数。
       		this.dataId=option.id
			console.log('id',this.dataId)
			this.getjackpotData()
       	},
		onShow() {
			
		},
        methods: {
			getjackpotData(){
				console.log("请求")
				
				jackpotDetail(this.dataId).then(res => {
					
					console.log(res.data)
					var list = []
					this.jackpotData = res.data.data;
					this.jackpotData.goodsList.forEach(ele=>{
						this.list.push({
							image:ele.goodsImg,
							// name:ele.goodsName.slice(0,5)
							name:ele.goodsSimple
						})
						
					})
					this.list.splice(4,0,{
					    image: '../../static/begin.png',
					    name: '开始抽奖',
					    percentage:-1, 
					    index_num:4
					})
					// items.splice(items.length / 2, 0, "hello");
					
					
				})
			},
            // 中奖后的逻辑
            updateMoney(prizeData) {
                // uni.showToast({
                //     title: '恭喜获得：' + this.AwardList[money].name,
                //     icon: "none",
                //     position: 'bottom'
                // })
				console.log("tantantatn")
				this.prizeData=prizeData
				this.prizeShow=true
                // 中奖后获取最新状态
                this.getData()
            },
            // 获取本次中奖的
            getData(){

                // 概率指定，如果是前端控制，如果后端控制下面步骤判断步骤可以省略  直接后端获取sjNum值即可
                if(this.isQd){
					
					
                    // ------------------------------这里写获取后端中奖索引
					// lottery(this.dataId).then(res => {
					// 	console.log('中奖',res.data)
					// 	this.prizeData=res.data.data
					// 	this.sjNum = this.prizeData.sort
					// 	console.log("this.sjNum")
					// 	console.log(this.sjNum)
					// })
                    // 后端赋值
                    
					
                }else{
                    // 概率控制  概率100%
                    let sj = Math.round(Math.random() * 99 + 1)
                    // 前端控制概率
                    let arr = []
                    console.log("sj: ",sj);

                    for (let i = 0; i < this.AwardList.length; i++) {
                        if(this.AwardList[i].percentage>=sj){
                            arr.push(this.AwardList[i])
                        }
                    }
                    console.log("arr: ",arr);
                    // 随机获得索引
                    if(arr.length>1){
                        this.sjNum = arr[Math.round(Math.random() * arr.length)].index_num
                    }else{
                        this.sjNum = arr[0].index_num
                    }

                    console.log(this.sjNum);
                }
            }

        }
    }
</script>
<style scoped>
	/deep/.u-mode-center-box{
		  background-image: linear-gradient(to bottom, rgb(204,203,243), rgb(254,254,254)) !important;
	}
	.prizeTittle{
		text-align: center;
		margin-top: 50rpx;
		font-size: 40rpx;
		font-weight: bold;
		color: rgb(81, 79, 223);
	}
</style>
